define(["../common"], function() {
	var addDoPath = g_shopDomain + "/storeGoods/addDo/json";

	loadjs(["pagectrl"]);
	loadjs(["goodsClassify"], function() {
		$("#selectClassify").goodsClassifyComboBox({
			value: $("#classifyId").val(),
			companyId: $("#hideData").html(),
			onSelect: function(deep, classify) {
				// 获取选中的最后一个有效值
				var value = $("#selectClassify select option:selected[value!='']:last").val();
				$("#classifyId").val(value);
			}
		});
	});

	// 全选
	$("[name='selectAll']").click(function() {
		$("[name='goodsIds']").prop("checked", $(this).prop("checked"));
		$("[name='selectAll']").prop("checked", $(this).prop("checked"));
	});

	// 事件：入库
	$("[id^='put_']").click(function() {
		var $that = $(this);
		if ($that.doing()) return; 
		
		var goodsId = $(this).attr("id").split("_")[1];
		$that.showMsg("入库中...", 'load', true);
		addDo($that.parents("tr"), goodsId, $that);
	});

	// 事件：批量入库
	$("#putSelected").click(function() {
		var $that = $(this);
		if ($that.doing()) return; 
		
		var $chekcedboxs = $(":checkbox[name='goodsIds']:checked");
		
		if ($chekcedboxs.length === 0) {
			$that.showMsg("请先选择商品", 'error');
			return;
		}
		
		var goodsIds = $chekcedboxs.map(function() {
			return $(this).val();
		}).get().join(",");
		
		$that.showMsg("批量入库中...", 'load', true);
		addDo($chekcedboxs.parents("tr"), goodsIds, $that);
	});

	function addDo($trs, goodsIds, $target) {
		$trs.fadeTo(null, 0.1);
		$.post(addDoPath, {goodsIds: goodsIds}, function(data) {
			if (data.status !== 0) {
				$target.showMsg(data.message, 'error');
				$trs.fadeTo('fast', 1);
				return;
			}
			if ($("#spgl tr:gt(0)").not($trs).length > 0) {
				$target.showMsg();
				$trs.fadeOut(function() {$(this).remove();});
			} else {
				$target.showMsg("入库成功!刷新列表...");
				setTimeout(function() {
    				pageReload.reload();
    			}, 1500);
			}
		});
	}

	// 一键入库相关
	$(function() {
		
		var num = 0;// 累计入库数
		var step = 0;// 每100毫秒入库数(计算值)
		var numIndex = -1;
		var threshold = 100;// 计数阀值
		var pageSize = 100;// 每次入库数
		var times = 0;// 累计提交次数
		
		// 事件:一键入库
	    $("#putAllBt").click(function() {
	    	var $this = $(this);
	    	var msg = "正在入库,请稍候...<span id='amount'>0</span>";
	    	if (!($this.showMsg(msg, 'load', true))) {
	    		return;
	    	}
	    	
	    	num = 0;
	    	times = 0;
	    	step = 1;// (10000/7000)默认为7秒提一次
	    	numIndex = setInterval(function() {
	    		num +=step;
	    		$("#amount").html(num);
	    	}, threshold);
	    	
	    	putAllDo_($this);
	    });
	    
	    function putAllDo_($this) {
	    	var startTime = new Date().getTime();
	    	// (ie兼容)加 r=Math.random() ,
	    	// ie由于请求参数不变使第二次不会请求到后台,并由于递归调用浏览器会"卡死"
	    	$.get(g_shopDomain + "/storeGoods/addAllDo/json?r=" + Math.random(), function(data) {
	    		if (data.status !== 0) {
	    			$this.showMsg(data.message, 'error');
	    			clearInterval(numIndex);
	    			return;
	    		}
	    		
	    		times++;
	    		num = parseInt((times - 1) * pageSize + data.result);
	    	
	    		if (data.result === pageSize) {// 每次入库数
	    			step = parseInt((pageSize * threshold)/(new Date().getTime() - startTime));
	    			putAllDo_($this);
	    		} else {
	    			clearInterval(numIndex);
	    			$this.showMsg("入库完成!", 'pass');
	    			setTimeout(function() {
	    				pageReload.reload();
	    			}, 1500);
	    		}
	    		
	    	});
	    }
	    
	});
});

